/*
 * @Date: 2023-03-16 10:15:55
 * @LastEditTime: 2023-04-12 14:51:26
 * @FilePath: /nlk/src/store/useBalanceStore.ts
 * 介绍:
 */
import { defineStore } from "pinia";
import * as web3Eth from "@@/sdk/web3Eth";
import * as web3Tron from "@@/sdk/web3Tron";
import { _require } from "@@/utils/tools/tools";
import { apiCoinGameBalance } from "@@/api/module/balance";
import { omit } from "lodash";

export default defineStore("StoreBalance", {
  //*全局仓库
  state: initState,
  //*全局函数
  actions: {
    async findGameBalance(coin: CoinInfo): Promise<void | number> {
      const res = await apiCoinGameBalance({ coin_id: coin.id });
      const currentCoin = this.ethCoinEnum[coin.code];
      if (!res) return;
      /**查询到的余额 */
      const gameBalcane = Number(res.availbale);
      if (!currentCoin) return gameBalcane;
      return (currentCoin.gameBalance = gameBalcane);
    },
    async findBalance(coin: CoinInfo): Promise<void | number> {
      if (window.isBnb) {
        const res = await web3Eth.sdkAutoFindBalance(
          coin.contract_addr,
          coin.format
        );
        const currentCoin = this.ethCoinEnum[coin.code];
        if (!currentCoin || !res.status) return res.data;
        return (currentCoin.balance = res.data);
      } else {
        const res = await web3Tron.sdkAutoFindBalance(
          coin.contract_addr,
          coin.format
        );
        const currentCoin = this.tronCoinEnum[coin.code];
        if (!currentCoin || !res.status) return res.data;
        return (currentCoin.balance = res.data);
      }
    },
  },
  //*计算属性
  getters: {
    coinEnum: (state) => {
      if (window.isBnb === true) return state.ethCoinEnum;
      if (window.isBnb === false) return state.tronCoinEnum;
      return {};
    },
  },
  // //*仓库数据缓存
  // persist: [
  //   {
  //     key: "StoreBalance",
  //     storage: localStorage, //指定储存位置
  //   },
  // ],
});
/**state类型定义 */
interface StoreBalance {
  ethCoinEnum: { [key: string]: StoreCoinItem | undefined };
  tronCoinEnum: { [key: string]: StoreCoinItem | undefined };
}
export interface StoreCoinItem extends CoinInfo {
  balance?: number;
  gameBalance?: number;
}
/**初始化pinia */
function initState(): StoreBalance {
  return {
    ethCoinEnum: omit(
      {
        USDT: {
          contract_addr: "0x55d398326f99059ff775485246999027b3197955",
          id: 1,
          code: "USDT",
          image: "",
          logo: _require("src/assets/images/coins/USDT.png"),
          format: 18,
          name: "USDT",
          price: 0,
        },
        NLK: {
          contract_addr: "0x8EfD6dc86b7e4E993B1a0Adaf611b5e4d6D48469",
          id: 5,
          code: "NLK",
          image: "",
          logo: _require("src/assets/images/coins/NLK.svg"),
          format: 6,
          name: "USDT",
          price: 0,
        },
        NLKS: {
          contract_addr: "0x0307ba07aea241fd650fd3e12f7ba7621774481a",
          id: 12,
          code: "NLKS",
          image: "",
          logo: _require("src/assets/images/coins/NLK.svg"),
          format: 6,
          name: "USDT",
          price: 0,
        },
        FIL: {
          contract_addr: "0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153",
          id: 6,
          code: "FIL",
          image: "",
          logo: _require("src/assets/images/coins/FIL.png"),
          format: 18,
          name: "FIL",
          price: 0,
        },
        BTC: {
          contract_addr: "0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c",
          id: 7,
          code: "BTC",
          image: "",
          logo: _require("src/assets/images/coins/BTC.png"),
          format: 18,
          name: "BTC",
          price: 0,
        },
        DOGE: {
          contract_addr: "0xba2ae424d960c26247dd6c32edc70b295c744c43",
          id: 8,
          code: "DOGE",
          image: "",
          logo: _require("src/assets/images/coins/DOGE.png"),
          format: 8,
          name: "DOGE",
          price: 0,
        },
        EOS: {
          contract_addr: "0x56b6fb708fc5732dec1afc8d8556423a2edccbd6",
          id: 9,
          code: "EOS",
          image: "",
          logo: _require("src/assets/images/coins/EOS.png"),
          format: 18,
          name: "EOS",
          price: 0,
        },
        SHIB: {
          contract_addr: "0x2859e4544c4bb03966803b044a93563bd2d0dd4d",
          id: 10,
          code: "SHIB",
          image: "",
          logo: _require("src/assets/images/coins/SHIB.png"),
          format: 18,
          name: "SHIB",
          price: 0,
        },
        USDK: {
          contract_addr: null,
          id: 13,
          code: "USDK",
          image: "",
          logo: _require("src/assets/images/coins/NLK.svg"),
          format: 6,
          name: "USDK",
          price: 0,
        },
      },
      JSON.parse(import.meta.env.VITE_EXCLUDE_ETH_COIN)
    ),
    tronCoinEnum: omit(
      {
        USDT: {
          contract_addr: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
          id: 1,
          code: "USDT",
          image: "",
          logo: _require("src/assets/images/coins/USDT.png"),
          format: 6,
          name: "USDT",
          price: 0,
        },
        TRX: {
          id: 4,
          code: "TRX",
          name: "TRX",
          image: "",
          contract_addr: null,
          format: 6,
          price: 0,
          logo: "",
        },
      },
      JSON.parse(import.meta.env.VITE_EXCLUDE_TRON_COIN)
    ),
  };
}
